#version 430 core
//out vec4 FragColor;

uniform float pointSize;
uniform mat4 projection;

in vec3 eyeSpacePos;
float LinearizeDepth(float depth)
{
    float near = 1;
    float far = 100.0;
    float z = depth * 2.0 - 1.0; // Back to NDC
    return (2.0 * near) / (far + near - z * (far - near));
}

void main(){	
	vec3 normal;
	normal.xy = gl_PointCoord.xy * vec2(1.0, -1.0) + vec2(-0.5,0.5);
	float mag = dot(normal.xy, normal.xy);
	if(mag > 1.0) discard;
	normal.z = sqrt(1.0 - mag);

	vec4 pixelEyePos = vec4(eyeSpacePos + normal * pointSize, 1.0f);
	vec4 pixelClipPos = projection * pixelEyePos;
	float ndcZ = pixelClipPos.z / pixelClipPos.w;

	//float linearDepth = LinearizeDepth(ndcZ);
	//FragColor = vec4(ndcZ,ndcZ,ndcZ, 1.0);//debug color
	gl_FragDepth = ndcZ;
}